package com.hz.us.service.impl;

import com.hz.api.model.dto.TeacherDTO;
import com.hz.us.mapper.UserMapper;
import com.hz.us.model.entity.UserEntity;
import com.hz.us.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    UserMapper userMapper;

    @Override
    public List<TeacherDTO> allTeachers() {
        List<UserEntity> userEntities = userMapper.getAllTeachers();
        List<TeacherDTO> teachers = new ArrayList<>();
        userEntities.forEach(userEntity -> {
            TeacherDTO teacherDTO = new TeacherDTO(userEntity.getId(), userEntity.getUsername(),
                    userEntity.getRealname(), userEntity.getStudent_id());
            teachers.add(teacherDTO);
        });
        return teachers;
    }

    @Override
    public TeacherDTO getTeacherById(Integer teacherId) {
        UserEntity userEntity = userMapper.selectTeacherById(teacherId);
        if (userEntity == null) {
            return null;
        }
        TeacherDTO teacherDTO = new TeacherDTO(userEntity.getId(), userEntity.getUsername(),
                userEntity.getRealname(), userEntity.getStudent_id());
        return teacherDTO;
    }
}
